
var hotelId = window.location.href;
    // hotelId =hotelId.split("?")[hotelId.split("?").length-1];  //获取到酒店id
    // hotelId = hotelId.split("=")[hotelId.split("=").length-1];
    hotelId=getQuery("hotelId");
    console.log(hotelId);
var sort=['all', 'praise', 'negative', 'picture'];
var sort_now="all";
var page=1;

var maxPage=1;
$(document).ready(function(){
    download();    //获取到酒店id的酒店信息   
    evaluateList(page,sort[0]);//全部评论
    
})
// 鼠标点击轮播图
$(".right_img").on("click","li",function(){
    var this_src=$(this).find("img")[0].src;
    // console.log(this_src);
    var big_src=$(".left_img").find("img")[0].src;
    // console.log(big_src);
    $(this).find("img").attr('src',big_src);
    $(".left_img").find("img").attr('src',this_src);
})

// 日历

		
var startTime,
    endTime,
    duringDay, startDate, endDate;
laydate.render({
    elem: '#enter_time', //指定元素
    format: 'yyyy年M月dd日',
    min: 0, //今天起
    max: 30, //30天后
    done: function(value, date) {
        startDate = value;
        str = date.year + '/' + date.month + '/' + date.date;
        var datex = new Date(str);
        var $totalTime = $('.totalTime');
        const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        $('#startDay').html(days[datex.getDay()]);
        startTime = parseInt(datex.getTime());
        var duringTime = endTime - startTime;
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            duringDay = parseInt(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }
        var nowTime=new Date();
        nowTime = parseInt(nowTime.getTime());
        if(startTime-nowTime<86400000){
            startTime+=86399000;
        }
    }
});
laydate.render({
    elem: '#leave_time', //指定元素
    format: 'yyyy年MM月dd日',
    min: 0, //今天起
    max: 30, //30天后
    done: function(value, date) {
        endDate = value;
        var str = date.year + '/' + date.month + '/' + date.date;
        var $totalTime = $('.totalTime'),
            datey = new Date(str);
        const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
        endTime = parseInt(datey.getTime());
        $('#endDay').html(days[datey.getDay()]);
        var duringTime = endTime - startTime;
        if (duringTime <= 0) {
            $totalTime.html('请选择入住日期以后的日期！')
        } else {
            duringDay = parseInt(duringTime / 86400000)
            $totalTime.html('共' + duringDay + '晚');
        }
    }
});
// 房型查询
$("#search_room").click(function(){
    var checkTime="";//预定时间段
    checkTime=startTime+","+endTime;
    console.log("checkTime"+checkTime);   


    if (!startTime) {
        var $star = $('#enter_time');
        $star.attr('placeholder', '请选择日期！')

    } else if (!endTime) {
        var $gone = $('#leave_time');
        $gone.attr('placeholder', '请选择日期！')
    } else {
        $(".mustChoose").remove();
        $.ajax({
            url:url+"v1/hotel/room",
            // url:"php/fangxing.js",
            method:"get",
            dataType:"json",
            data:{
                hotelId:hotelId,
                checkTime:checkTime
            },
            xhrFields: {
                withCredentials: true // 允许跨域名储存和访问cookie
            }
        }).done(function(data){
            console.log(data);
            $(".room_detail .room_list").remove();
            var roomData=data.data.roomList;
            console.log(data);
            var str="";
            var reserveBtn="";

            var Check_out_time=$("#leave_time").val();//退房时间
            var Check_data_time=$("#enter_time").val();//入住时间
            var Check_out_data_time=endTime;//退房时间戳
            var Check_data_data_time=startTime;//入住时间戳
            var Check_week=$("#startDay").html();//入住星期
            var Check_out_week=$("#endDay").html();//退房星期

            for(i=0;i<roomData.length;i++){
                if(roomData[i].canCheckIn==true){                    
                    // url_Jump = `<a href="create.html?酒店ID=`+ hotelId +`?房间数量=`+ roomData[i].count +`?房间名称=`+ roomData[i].name +`?房间价格=`+ roomData[i].price +`?酒店位置=`+ localStorage.jiudian_HotelPosition +`?酒店名称=`+ localStorage.jiudian_HotelName +`?床型=`+ roomData[i].bedType+`?房间面积=`+ roomData[i].area +`?WIFI=`+ roomData[i].wayOfInternet +`?早餐=`+ roomData[i].breakfast +`?酒店电话=`+ localStorage.jiudian_HotelPhone +`?入住时间=`+ Check_data_time +`?退房时间=`+ Check_out_time +`?入住时间戳=`+ Check_data_data_time +`?退房时间戳=`+ Check_out_data_time +`?入住星期几=`+ Check_week +`?退房星期几=`+ Check_out_week +`?酒店照片=`+localStorage.jiudian_HotelImg +`" class="reserve">预定</a>`;
                    reserveBtn='<a class="reserve" href="create.html?id='+ hotelId+'?RoomCount='+ roomData[i].count+'?RoomName='+roomData[i].name+'?RoomPrice='+roomData[i].price+'?position='+localStorage.jiudian_HotelPosition+'?HotelName='+localStorage.jiudian_HotelName+'?BedType='+roomData[i].bedType+'?RoomArea='+roomData[i].area+'?wayOfInternet='+roomData[i].wayOfInternet+'?breakfast='+roomData[i].breakfast+'?HotelPhone='+localStorage.jiudian_HotelPhone+'?Check_data_time='+Check_data_time+'?Check_out_time='+Check_out_time+'?Check_data_data_time='+Check_data_data_time+'?Check_out_data_time='+Check_out_data_time+'?Check_week='+Check_week+'?Check_out_week='+Check_out_week+'?Hotel_picture='+localStorage.jiudian_HotelImg+'">预订</a>'
                }else if(roomData[i].canCheckIn==false){
                    reserveBtn='<a class="without">已订完</a>'
                    $(".with").css("display","block");
                }
                str+=`<ul class="room_list clearfix">
                        <li class="room_name">`+ roomData[i].name+`</li>
                        <li class="bedType">`+ roomData[i].bedType+`</li>
                        <li class="room_area"><span>`+ roomData[i].area+`</span>M²</li>
                        <li class="wayOfInternet">`+ roomData[i].wayOfInternet+`</li>
                        <li class="breakfast">`+ roomData[i].breakfast+`</li>
                        <li class="room_window">`+ roomData[i].window+`</li>
                        <li class="cancelOfRules">`+roomData[i].cancelOfRules+`</li>
                        <li class="room_price">￥<span>`+ roomData[i].price+`</span></li>
                        <li class="canCheckIn">`+reserveBtn+`</li>
                      </ul>`
            }
            $(".room_detail").append(str);

        })
    }
})

function download(){
    $.ajax({
        url:url+"v1/hotel/detail",
        // url:"php/jiudianxiangqing.js",
        method:"get",
        dataType:"json",
        data:{
            hotelId:hotelId
        },
        xhrFields: {
            withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        console.log(data);
        var a=data.data.hotel;
        if(data.code="success"){
            var star_num="";//保存星的数量
            var star_num_str="";
            star_num=a.star_level;
            for(j=0;j<star_num;j++){
                star_num_str+='<img src="img/icon_star.png">';
            }
            $(".description").find("div").remove();
            console.log(data);
            // description酒店名 酒店位置

            // 酒店位置
            localStorage.jiudian_HotelPosition=a.address;
            // 酒店名字
            localStorage.jiudian_HotelName=a.name;
            // 酒店电话
            localStorage.jiudian_HotelPhone=a.hotel_introductio.contact_way[0];
            // 酒店图片
            localStorage.jiudian_HotelImg=a.picture[0];
            var str="";
            str+=`<div class="hotel_name lf">
                    <p class="hotel_title clearfix">
                        <span class="lf">`+ a.name +`</span>
                        <span class="gt">`+ star_num_str +`</span>
                    </p>
                    <p class="address">
                        `+ a.address +`<a>-地图</a>
                    </p>
                </div>
                <div class="hotel_remark lf">
                    <p>
                        <span class="score">`+ a.overall_rating +`</span>/5分
                    </p>
                    <p>
                        <span class="comment_num">`+ a.comment_num +`</span>次点评
                    </p>
                </div>
                <div class="price lf">
                    <p>
                        <span class="hotel_price">￥`+ a.price +`</span>起
                    </p>
                </div>
                <div class="wechat gt" id="weChat">
                    <img src="img/icon_share.png" class="share">微信
                    <div class="mask">
                        使用微信扫一扫
                        <img src="https://dev.apis.sh/PBMKFkON0/v1/hotel/share">
                    </div>
                </div>`;
            $(".description").append(str);            


            $(".wechat").click(function(){
                $(".mask").toggle();
            })

            // 酒店图片
            $(".bigPic").attr("src",url_file+a.picture[0]);
            var strBanner="";
            for(i=1;i<a.picture.length;i++){
                $(".right_img").find("li").remove();
                strBanner+='<li><img src='+url_file + a.picture[i]+'></li>';
                $(".right_img").append(strBanner);
            }


            // hotel_introduce酒店简介
            $(".hotel_introduce").find("div").remove();
            var str2="";
            str2+=`<div class="hotel_introduce_detail">
                        <div class="hotel_introduce_title">酒店介绍</div>
                        <div class="hotel_introduce_content">
                            <span>`+ a.hotel_introductio.basic_info +`</span>
                            <span>`+ a.hotel_introductio.contact_way +`</span>
                            <p>`+ a.hotel_introductio.detail +`</p>
                        </div>
                   </div>
                   <div class="hotel_facilities">
                        <div class="hotel_facilities_title">酒店设施</div>
                        <div class="integrated">
                            <ul class="hotel_integrated clearfix">
                                <li>综合设施</li>
                            </ul>
                        </div>
                        <div class="hotel_facilities_room">
                            <ul class="hotel_room clearfix">
                                <li>客房设施</li>
                            </ul>
                        </div>
                        <div class="service_items">
                            <ul class="hotel_service clearfix">
                                <li>服务项目</li>
                            </ul>
                        </div>
                        <div class="activity">
                            <ul class="hotel_activity clearfix">
                                <li>活动设施</li>
                            </ul>
                        </div>
                   </div>
                   <div class="hotel_policy">
                        <div class="hotel_policy_title">
                            酒店政策
                        </div>
                        <div class="entry_time">
                            <ul class="clearfix">
                                <li>入店和离店</li>
                                <li>
                                    <span>`+a.hotel_policy.entry_time+`</span>
                                    <span>`+a.hotel_policy.departure_time+`</span>
                                </li>
                            </ul>
                        </div>
                        <div class="child_policy">
                            <ul class="clearfix">
                                <li>儿童政策</li>
                                <li>`+a.hotel_policy.child_policy +`</li>
                            </ul>
                        </div>
                        <div class="pet_policy">
                            <ul class="clearfix">
                                <li>宠物</li>
                                <li>`+a.hotel_policy.pet_policy+`</li>
                            </ul>
                        </div>
                   </div>`;

            $(".hotel_introduce").append(str2);
            
            // 酒店设施——综合设施
            var integrated="";
            for(b=0;b<a.hotel_facilities.integrated.length;b++){
                integrated+='<li>'+a.hotel_facilities.integrated[b]+'</li>';
            }
            $(".hotel_integrated").append(integrated);
            // 酒店设施——客房设施
            var hotel_room="";
            for(b=0;b<a.hotel_facilities.room.length;b++){
                hotel_room+='<li>'+ a.hotel_facilities.room[b] +'</li>';
            }
            $(".hotel_room").append(hotel_room);
            // 酒店设施——服务项目
            var hotel_service="";
            for(b=0;b<a.hotel_facilities.service_items.length;b++){
                hotel_service+='<li>'+a.hotel_facilities.service_items[b]+'</li>';
            }
            $(".hotel_service").append(hotel_service);
            // 酒店设施——活动设施
            var hotel_activity="";
            for(b=0;b<a.hotel_facilities.activity.length;b++){
                hotel_activity+='<li>'+a.hotel_facilities.activity[b]+'</li>';
            }
            $(".hotel_activity").append(hotel_activity);


            const map = new BMap.Map("container") // 创建一个地图实例，其参数可以是元素id也可以是元素对象
            map.centerAndZoom(new BMap.Point(a.location.lng, a.location.lat ), 13) // 初始化地图，设置中心点坐标和地图级别
            map.enableScrollWheelZoom(true) // 启用滚轮放大缩小，默认禁用
            map.addControl(new BMap.ScaleControl()) // 添加控件，比例尺控件
            map.addControl(new BMap.NavigationControl({
            type: BMAP_NAVIGATION_CONTROL_ZOOM
            })) // 添加控件，平移缩放控件，type值表示只显示控件的缩放部分功能
            console.log(a);
            const hotelDataArry = [
                {
                    name: a.name ,
                    location: new BMap.Point( a.location.lng , a.location.lat )
                }
            ]
            hotelDataArry.forEach(el => {
            const marker = new BMap.Marker(el.location) // 创建标注点
            map.addOverlay(marker) // 向地图添加标注点
            marker.setLabel(new BMap.Label(el.name, {
                offset: new BMap.Size(20)
            })) // 向标注点添加标注文本
            })

        }
    })

}

// 全部评论1
function evaluateList(page,sort,maxPage){
    console.log(sort);
    $.ajax({
        // url:"php/pinglun.js",
        url:url+"v1/hotel/evaluate/list",
        dataType:"json",
        method:"get",
        data:{
            hotelId:hotelId,
            limit:3,
            page:page,
            sort:sort
        }, // 按接口文档要求传参
        xhrFields: {
          withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        console.log(data);
        $(".discuss_user").find("ul").remove();
        var str="";
        var evaluates=data.data.evaluate;
        maxPage=Math.ceil(data.data.count/3);
        console.log(maxPage);
        if(data.code=="success"){
            for(i=0;i<evaluates.length;i++){
                var evaluates_img="";
                var star_num_str="";
                // 判断是否有图
                if(evaluates[i].picture.length!=0){
                    for(j=0;j<evaluates[i].picture.length;j++){
                        evaluates_img+='<li><img src="'+url_file+evaluates[i].picture[j]+'"></li>'
                    }
                }
                // 用户评价星级
                for(b=0;b<evaluates[i].score;b++){
                    star_num_str+='<img src="img/icon_star.png">'
                }
                str+=`<ul>
                        <li class="clearfix">
                            <div class="other_user">`
                                +`<img src="`+url_file+evaluates[i].account.avatar+`" class="other_avatar">`+
                                // <img src="img/1.jpg" class="other_avatar">
                                `
                                <span class="other_userName">`+evaluates[i].account.name+`</span>
                            </div>
                            <div class="discuss_content clearfix">
                                <p class="discuss_score">
                                    `+star_num_str+`
                                    <span class="poshed_time gt">`+moment(evaluates[i].create_time).format('YYYY-MM-DD')+`</span>
                                </p>
                                <p class="evaluate">
                                    `+evaluates[i].content+`
                                </p>
                                <div class="discuss_pic">
                                    <ul class="clearfix">
                                        ${evaluates_img}
                                    </ul>
                                </div>
                            </div>
                        </li>
                      </ul>`;                
            }
            $(".discuss_user").append(str);

            // 分页器

            $('.box').pagination({
                pageCount:maxPage,
                jump: true,
                coping: true,
                homePage: '首页',
                endPage: '末页',
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api.getCurrent())
                    
                    page = api.getCurrent();
                    evaluateList2(page,sort_now);
                }
                }
            )
        }
    })

}
// 全部评论
function evaluateList2(page,sort,maxPage){
    console.log(sort);
    $.ajax({
        // url:"php/pinglun.js",
        url:url+"v1/hotel/evaluate/list",
        dataType:"json",
        method:"get",
        data:{
            hotelId:hotelId,
            limit:3,
            page:page,
            sort:sort
        }, // 按接口文档要求传参
        xhrFields: {
          withCredentials: true // 允许跨域名储存和访问cookie
        }
    }).done(function(data){
        console.log(data);
        $(".discuss_user").find("ul").remove();
        var str="";
        var evaluates=data.data.evaluate;
        maxPage=Math.ceil(data.data.count/3);
        console.log(maxPage);
        if(data.code=="success"){
            for(i=0;i<evaluates.length;i++){
                var evaluates_img="";
                var star_num_str="";
                // 判断是否有图
                if(evaluates[i].picture.length!=0){
                    for(j=0;j<evaluates[i].picture.length;j++){
                        evaluates_img+='<li><img src="'+url_file+evaluates[i].picture[j]+'"></li>'
                    }
                }
                // 用户评价星级
                for(b=0;b<evaluates[i].score;b++){
                    star_num_str+='<img src="img/icon_star.png">'
                }

                str+=`<ul>
                        <li class="clearfix">
                            <div class="other_user">`
                                // <img src="`+url_file+evaluates[i].account.avatar+`" class="other_avatar">
                                +`<img src="img/1.jpg" class="other_avatar">`+
                                `
                                <span class="other_userName">${evaluates[i].account.name}</span>
                            </div>
                            <div class="discuss_content clearfix">
                                <p class="discuss_score">
                                    `+star_num_str+`
                                    <span class="poshed_time gt">`+moment(evaluates[i].create_time).format('YYYY-MM-DD')+`</span>
                                </p>
                                <p class="evaluate">
                                    `+evaluates[i].content+`
                                </p>
                                <div class="discuss_pic">
                                    <ul class="clearfix">
                                        ${evaluates_img}
                                    </ul>
                                </div>
                            </div>
                        </li>
                      </ul>`;
            }
            $(".discuss_user").append(str);            
        }
    })

}
// 评论排序
$(".discuss_nav").on("click","li",function(){
    $(this).css({"background":"#5944C3","color":"#fff"}).siblings().css({"background":"#F1F1F1","color":"#3E3E3E"})
    var data_sort=$(this).attr("data-sort");
    if(data_sort=="all"){
        sort_now=sort[0];
        page=1;
        evaluateList(page,sort_now)
        console.log("all");

    }else if(data_sort=="praise"){
        sort_now=sort[1];
        page=1;
        evaluateList(page,sort_now)
        console.log("praise");
    }else if(data_sort=="negative"){        
        sort_now=sort[2];
        page=1;
        evaluateList(page,sort_now)
        console.log("negative");
    }else if(data_sort=="picture"){
        sort_now=sort[3];
        page=1;
        evaluateList(page,sort_now)
        console.log("picture");
    }
})

